package com.bw.day03.fg

import androidx.lifecycle.viewModelScope
import com.bw.base.net.entity.CarEntity
import com.bw.base.net.entity.PreOrderEntity
import com.bw.base.net.entity.Res
import com.bw.base.net.mvvm.BaseViewModel
import com.bw.base.net.param.ParamPreOrder
import com.bw.day03.utlis.SingleLiveData
import kotlinx.coroutines.launch

class CarViewModel: BaseViewModel() {
    val carSuccess= SingleLiveData<Res<CarEntity>>()
    val carFail= SingleLiveData<String>()
    fun car(isValid:String,limit:String,page:String){
        viewModelScope.launch {
            try {
                carSuccess.postValue(apiService.car(isValid,limit,page))
            }catch (e:Exception){
                carFail.postValue(e.message.toString())
            }
        }
    }

    val updateSuccess= SingleLiveData<Res<String?>>()
    val updateFail= SingleLiveData<String>()
    fun updateCar(id:Int,number:Int){
        viewModelScope.launch {
            try {
                updateSuccess.postValue(apiService.updateCar(id,number))
            }catch (e:Exception){
                updateFail.postValue(e.message.toString())
            }
        }
    }
    val deleteSuccess= SingleLiveData<Res<String?>>()
    val deleteFail= SingleLiveData<String>()
    fun deleteCar(ids:Array<Int>){
        viewModelScope.launch {
            try {
                deleteSuccess.postValue(apiService.deleteCar(ids))
            }catch (e:Exception){
                deleteFail.postValue(e.message.toString())
            }
        }
    }
    val preSuccess= SingleLiveData<Res<PreOrderEntity>>()
    val preFail= SingleLiveData<String>()
    fun preOrder(paramPreOrder: ParamPreOrder){
        viewModelScope.launch {
            try {
                preSuccess.postValue(apiService.preOrder(paramPreOrder))
            }catch (e: Exception){
                preFail.postValue( e.message.toString())
            }
        }
    }
}